package model.siw
{
	import flash.events.ContextMenuEvent;
	import flash.events.Event;
	import flash.ui.ContextMenu;
	import flash.ui.ContextMenuItem;
	
	import mx.controls.Label;
	
	public class TranFigure extends Label{
		
		/*
		 三种类型:
		 1. S:E1后, 最终会为R:E2             end
		 2. S:E1后, 紧跟着会为R:E2           next
		 3. S:E1后, R:E2, 并且两者具有相关性   interrelated
		*/
		private var _tranType:String;
		
		private var _col:int;
		
		private var _oldRuleFigure:RuleFigure;
		
		private var _newRuleFigure:RuleFigure;
		
		private var normalItem:ContextMenuItem;
		
		private var circleItem:ContextMenuItem;
		
		private var dependItem:ContextMenuItem;		
		
		private var concelItem:ContextMenuItem;		
		
		public function TranFigure(){
			createChildren();
			/* this.textField.background=true;
			this.textField.backgroundColor=0xEEFFFF; */
			this.initialize();
			initContextMenu();
		}
		
		public function initContextMenu(){
					
			this.contextMenu= new ContextMenu();
			this.contextMenu.hideBuiltInItems();
			
			
			normalItem = new ContextMenuItem("最终关联");
			circleItem = new ContextMenuItem("顺序关联");
			dependItem = new ContextMenuItem("相关性关联");
			concelItem = new ContextMenuItem("取消关联");
			
			normalItem.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, normalDispose);
			circleItem.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, circleDispose);
			dependItem.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, dependDispose);
			concelItem.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, concelDispose);
			
			this.contextMenu.customItems.push(normalItem);
			this.contextMenu.customItems.push(circleItem);
			this.contextMenu.customItems.push(dependItem);
		
		}
		
		public function concelDispose(event:Event){
			this.graphics.clear();
			this._tranType="non";
		}
		
		public function normalDispose(event:Event){
			this.graphics.clear();
			this._tranType="end";
			drawLineDefault(_oldRuleFigure.width, _col, 20+_oldRuleFigure.height/2);
		}
		
		public function circleDispose(event:Event){
			this.graphics.clear();
			this._tranType="next";
			drawLineCircle(_oldRuleFigure.width, _col, 20+_oldRuleFigure.height/2);
		}
		
		public function dependDispose(event:Event){
			this.graphics.clear();
			this._tranType="interrelated";
			drawLineDepend(_oldRuleFigure.width, _col, 20+_oldRuleFigure.height/2);
		}
		
		private function drawLineDefault(oldX:int, newX:int, y){
			
			this.graphics.lineStyle(2,0x000000);
			this.graphics.moveTo(oldX, y);
			this.graphics.lineTo(newX-6, y);
			
			this.graphics.beginFill(0xFF0000);
			this.graphics.moveTo(newX,y);
			this.graphics.lineTo(newX-6,y-3);
			this.graphics.lineTo(newX-6,y+3);
			this.graphics.lineTo(newX,y);
			this.graphics.endFill();	
			
		}
		
		private function drawLineCircle(oldX:int, newX:int, y){
			
			this.graphics.lineStyle(2,0x000000);
			this.graphics.moveTo(oldX, y);
			this.graphics.lineTo((oldX+newX)/2-3, y);	
			this.graphics.moveTo((oldX+newX)/2+3, y);
			this.graphics.lineTo(newX-6, y);			
			
			this.graphics.beginFill(0x000000);
			this.graphics.moveTo(newX,y);
			this.graphics.lineTo(newX-6,y-3);
			this.graphics.lineTo(newX-6,y+3);
			this.graphics.lineTo(newX,y);
			this.graphics.endFill();	
			
			this.graphics.drawCircle((oldX+newX)/2, y, 3);
		}
		
		private function drawLineDepend(oldX:int, newX:int, y:int){
			
			this.graphics.lineStyle(2,0x000000);
			this.graphics.moveTo(oldX, y);
			this.graphics.lineTo((oldX+newX)/2-5, y);	
			this.graphics.moveTo((oldX+newX)/2+5, y);
			this.graphics.lineTo(newX-6, y);			
			
			this.graphics.beginFill(0x000000);
			this.graphics.moveTo(newX,y);
			this.graphics.lineTo(newX-6,y-3);
			this.graphics.lineTo(newX-6,y+3);
			this.graphics.lineTo(newX,y);
			this.graphics.endFill();	
			
			this.graphics.drawCircle((oldX+newX)/2, y, 5);			
				
			dranDashedLine(oldX-_oldRuleFigure.width/2,newX+_newRuleFigure.width/2,20);	
			
		}
		
		private function dranDashedLine(oldX:int, newX:int, y){
			var space:int=2;
			var dashed:int=4;
			var i:int;
			
			this.graphics.lineStyle(1,0x000000);
			
			for(i=oldX;i<newX;){				
				this.graphics.moveTo(i, y-15);
				this.graphics.lineTo(i+dashed,y-15);
				i+=(dashed+space);				
			}
			
			for(i=y-15;i<y;){				
				this.graphics.moveTo(oldX, i);
				this.graphics.lineTo(oldX, i+dashed);				
				this.graphics.moveTo(newX, i);
				this.graphics.lineTo(newX, i+dashed);				
				i+=(dashed+space);	
			}
			
			this.graphics.beginFill(0x000000);
			this.graphics.moveTo(oldX,y);
			this.graphics.lineTo(oldX+3,y-6);
			this.graphics.lineTo(oldX-3,y-6);
			this.graphics.lineTo(oldX,y);
			this.graphics.endFill();	
			
		}
		
		public function set oldRuleFigure(v:RuleFigure){
			this._oldRuleFigure=v;
		}
		
		public function get oldRuleFigure():RuleFigure{
			return this._oldRuleFigure;
		}
		
		public function set newRuleFigure(v:RuleFigure){
			this._newRuleFigure=v;
		}
		
		public function get newRuleFigure():RuleFigure{
			return this._newRuleFigure;
		}
		
		public function set col(v:int){
			this._col=v;
		}
		
		public function get col():int{
			return this._col;
		}
		
		public function set tranType(v:String){
			this._tranType=v;
		}
		
		public function get tranType():String{
			return this._tranType;
		}

	}
}